"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommonModule = void 0;
const common_1 = require("@nestjs/common");
const typeorm_1 = require("@nestjs/typeorm");
const config_1 = require("@nestjs/config");
const path_1 = __importDefault(require("path"));
const user_entity_1 = require("../entities/user.entity");
const essay_entity_1 = require("../entities/essay.entity");
/**
 * 加载配置文件
 */
const file_1 = __importDefault(require("../config/file"));
const index_1 = __importDefault(require("../config/index"));
// 引入使用
const dotenv = __importStar(require("dotenv"));
const envFilePath = `.env.${process.env.NODE_ENV || `development`}`;
const Joi = __importStar(require("@hapi/joi"));
const passport_1 = require("@nestjs/passport");
const jwt_1 = require("@nestjs/jwt");
const constants_1 = require("../passport/constants");
const local_strategy_1 = require("../passport/local.strategy");
const jwt_strategy_1 = require("../passport/jwt.strategy");
let CommonModule = class CommonModule {
};
exports.CommonModule = CommonModule;
exports.CommonModule = CommonModule = __decorate([
    (0, common_1.Global)(),
    (0, common_1.Module)({
        imports: [
            //1.配置config目录
            config_1.ConfigModule.forRoot({
                cache: true,
                envFilePath,
                load: [
                    () => dotenv.config({
                        path: '.env',
                    }),
                    index_1.default,
                    file_1.default,
                ],
                ignoreEnvFile: false,
                //忽略配置文件，为true则仅读取操作系统环境变量，常用于生产环境
                isGlobal: true,
                //配置为全局可见，否则需要在每个模块中单独导入ConfigModule
                validationSchema: Joi.object({
                    ENV: Joi.string()
                        .valid('development', 'production', 'test', 'provision')
                        .default('development'),
                }),
            }),
            //2.读取配置，这里读取的是数据库配置
            typeorm_1.TypeOrmModule.forRootAsync({
                imports: [config_1.ConfigModule],
                inject: [config_1.ConfigService],
                useFactory: (config) => {
                    console.log(config.get('db.mysql'));
                    return Object.assign({ entities: [path_1.default.resolve(__dirname, '../**/**.entity{.ts,.js}')] }, config.get('db.mysql'));
                },
            }),
            passport_1.PassportModule.register({ defaultStrategy: 'local' }),
            jwt_1.JwtModule.register({
                secret: constants_1.jwtConstants.secret,
                signOptions: { expiresIn: '24h' },
            }),
            typeorm_1.TypeOrmModule.forFeature([user_entity_1.UsersEntity, essay_entity_1.EssayEntity]),
        ],
        controllers: [],
        providers: [local_strategy_1.LocalStrategy, jwt_strategy_1.JwtStrategy],
        exports: [],
    })
], CommonModule);
